<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户密码修改</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">

    <div id="test12" class="demo-tree-more"></div>
    <div class="layui-btn">
        <button type="button" class="layui-btn layui-btn-sm" lay-demo="getChecked">角色确认保存</button>
    </div>

    <hr>
    <hr>

    <div class="layui-form-item">
        <label class="layui-form-label ">ID</label>
        <div class="layui-input-block">
            <input id="id" type="text" readonly name="id"   value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label ">用户名</label>
        <div class="layui-input-block">
            <input id="userName" type="text" readonly name="userName"   value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label ">密码</label>
        <div class="layui-input-block">
            <input type="text" id="userPassword" name="userPassword"  value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">修改确认保存</button>
        </div>
    </div>

</div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../js/jquery-3.5.1.min.js"></script>

<script>
    function child(obj){
        console.log(obj);
        $("#id").val(obj.id);
        $("#userName").val(obj.userName);
        $("#userPassword").val(obj.userPassword);
    }
</script>

<script>
    layui.use(['form','tree','util'], function () {
        var form = layui.form,
            layer = layui.layer,
            tree=layui.tree,
            util = layui.util,
            $ = layui.$;

        /**
         * 获取到
         */
        var userid = window.localStorage.getItem("userid");
        $.ajax({
            url:'/role/findRoleByUser?id='+userid,
            dataType:'json',
            success:function (res){
                if (res.code===0){
                    tree.render({
                        elem: '#test12'
                        ,data: res.data
                        ,showCheckbox: true  //是否显示复选框
                        ,id: 'demoId1'
                        ,isJump: true //是否允许点击节点时弹出新窗口跳转
                    });
                }
            }
        });

        //基本演示
        //按钮事件
        util.event('lay-demo', {
            getChecked: function (othis) {
                var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据

                //1.checkedData 用户前端选中的权限
                //2.传输参数:1.perlist 权限列表。2.角色的id
                //3.在中间表中删除掉 该角色的所有的权限的关联关系

                console.log(checkedData);

                $.ajax({
                    url:'/user/updateRole',
                    data:JSON.stringify({rollist:checkedData,userid:userid}),
                    contentType:"application/json",
                    type:"post",
                    dataType: 'json',
                    success:function (res){
                        if (res.code==0){
                            alert(res.msg);
                        }
                    }
                })
            }
        });


         //监听提交
         form.on('submit(saveBtn)', function (data) {
             $.ajax({
                 url:'/user/updateUser',
                 data:JSON.stringify(data.field),
                 type:"post",
                 contentType:"application/json",
                 dataType:"json",
                 success:function (res){
                     if (res.code===0){
                         alert(res.msg);
                         var iframeIndex = parent.layer.getFrameIndex(window.name);
                         parent.layer.close(iframeIndex);
                     }
                 }
             });
             return false;
         });

    });
</script>
</body>
</html>